package page.ineffable.photostorage.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import page.ineffable.photostorage.mapper.ImageMapper;
import page.ineffable.photostorage.pojo.Photo;
import page.ineffable.photostorage.service.PhotoService;
import page.ineffable.photostorage.util.FileUtils;
import page.ineffable.photostorage.util.UUIDUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 文件上传接口实现
 *
 * @author lqs2
 * @date 2018/10/28, Sun
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PhotoServiceImpl implements PhotoService {

    private static final Logger log = LoggerFactory.getLogger(PhotoServiceImpl.class);

    /**
     * 相册根路径
     **/
    @Value("${image.storage.path}")
    private String imageRootPath;

    private final ImageMapper imageMapper;


    @Autowired
    public PhotoServiceImpl(ImageMapper imageMapper) {
        this.imageMapper = imageMapper;
    }


    @Override
    public Photo uploadPhoto(String username, String filename, byte[] bytes) {
        if (bytes == null) {
            log.error("bytes[] is null");
            throw new NullPointerException();
        }
        Photo photo = new Photo(UUIDUtils.getUUID(), filename, username);
        photo.setSize(bytes.length);
        photo.setSuffix(filename.substring(filename.lastIndexOf("\\.")) + 1);
        File file = new File(FileUtils.calImagePath(filename, imageRootPath));
        FileOutputStream fops = null;
        try {
            fops = new FileOutputStream(file);
            fops.write(bytes);
        } catch (IOException e) {
            try {
                assert fops != null;
                fops.close();
            } catch (IOException e1) {
                log.error("", e1);
            }
            log.error("", e);
        }
        return photo;
    }

    @Override
    public Photo uploadPhoto(String username, String filename, MultipartFile file) {
        Photo photo = new Photo(UUIDUtils.getUUID(), filename, username);
        photo.setSuffix(filename.substring(filename.lastIndexOf(".") + 1));
        String relativePath = FileUtils.calImagePath(filename, imageRootPath);
        photo.setStoragePath(relativePath);
        File destFile = new File(imageRootPath + relativePath);
        try {
            photo.setSize(file.getBytes().length);
            file.transferTo(destFile);
            int i = imageMapper.insertRecord(photo);
            if (i == 0) {
                photo = null;
            }
        } catch (IOException e) {
            log.error("", e);
            photo = null;
        }
        return photo;
    }

    @Override
    public List<Photo> getPhotos(String username) {
        return imageMapper.getPhotosByUser(username);
    }

    @Override
    public boolean dropPhoto(String id, String username) {
        return imageMapper.removePhotoById(id, username) == 1;
    }

    @Override
    public Photo getPhotoById(String id) {
        return imageMapper.getPhotosById(id);
    }

    @Override
    public String getImageRootPath() {
        return imageRootPath;
    }

    @Override
    public boolean modImageMarkState(String id, int state) {
        return imageMapper.modPhotoWaterMarkState(id, state) == 1;
    }

    @Override
    public boolean modImageInfo(String id, Photo photo) {
        return imageMapper.modPhotoInfoAfterAddMark(id, photo) == 1;
    }
}
